package com.aaa.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/6/14 14:07
 * @description：日志统一处理
 * @modified By：
 * @version: 1.0
 */
@WebFilter(urlPatterns = "/*")
public class LogFilter  implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //增强处理，添加日志
        String method = request.getParameter("method");
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        Object username = httpServletRequest.getSession().getAttribute("username");
        if(null!=method){
            System.out.println("日志：用户"+username+new Date()+"执行了"+method);
        }
        //放行
        chain.doFilter(request,response);
    }
}
